package com.enterprise.projectManager.service.implemention;

import com.enterprise.projectManager.dto.CheckSearchConditions;
import com.enterprise.projectManager.model.QualityCheck;
import com.enterprise.projectManager.rope.QualityCheckRope;
import com.enterprise.projectManager.service.QualityCheckService;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
@RequiredArgsConstructor
@Transactional
public class QualityCheckImpl implements QualityCheckService {
    private final QualityCheckRope qualityRope;
    private final CountServiceImpl countService;

    @Override
    public QualityCheck createQual(QualityCheck quality) {
        if (quality.getCheckId() == null) quality.setCheckId(countService.generalQualityCheckId());
        return qualityRope.save(quality);
    }

    @Override
    public Boolean deleteQual(String id3) {
        Optional<QualityCheck> optionalQuality = qualityRope.findById(id3);
        qualityRope.delete(optionalQuality.get());
        return Boolean.TRUE;    //待进一步判断
    }

    @Override
    public QualityCheck updateQual(QualityCheck quality) {
        return qualityRope.save(quality);
    }

    @Override
    public QualityCheck searchQualById(String id3) {
        Optional<QualityCheck> optionalQuality = qualityRope.findById(id3);
        return optionalQuality.orElse(new QualityCheck()); // 或者根据实际需求返回其他默认值
    }

    @Override
    public QualityCheck searchQualityByResult(String Result) {
        Optional<QualityCheck> optionalQuality = qualityRope.findById(Result);
        return optionalQuality.orElse(null); // 或者根据实际需求返回其他默认值
    }
    @Override
    public QualityCheck searchQualityByDate(String Dates) {
        Optional<QualityCheck> optionalQuality = qualityRope.findById(Dates);
        return optionalQuality.orElse(null); // 或者根据实际需求返回其他默认值
    }

    @Override
    public List<QualityCheck> listQual(int limitqual) {
        // 从数据库中检索前 limitQual 个质量检测
        return qualityRope.findAll(PageRequest.of(0, limitqual)).getContent();
    }

    @Override
    public List<QualityCheck> listAll() {
        return qualityRope.findAll();
    }

    @Override
    public List<QualityCheck> filterQualityCheckBySQL(CheckSearchConditions checkSearchConditions) {
        System.out.println(checkSearchConditions);
        return qualityRope.filterQualityCheck(
                checkSearchConditions.getCheckId(),
                checkSearchConditions.getCheckResult(),
                checkSearchConditions.getCheckDate() == null ? null : (checkSearchConditions.getCheckDate().length == 0 ? null : checkSearchConditions.getCheckDate()[0]),
                checkSearchConditions.getCheckDate() == null ? null : (checkSearchConditions.getCheckDate().length <= 1 ? null : checkSearchConditions.getCheckDate()[1]),
                checkSearchConditions.getProjectId()
        );
    }

}

